import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";
import _isUndefined from "lodash/isUndefined";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class, _class2;

/**
 * MessageTemplate - 消息模板列表
 * @date: 2018-7-26

 * @version: 1.0.0

 */
import React, { Component } from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Bind, Debounce } from 'lodash-decorators';
import { Header, Content } from 'components/Page';
import { Button as ButtonPermission } from 'components/Permission';
import formatterCollections from 'utils/intl/formatterCollections';
import notification from 'utils/notification';
import intl from 'utils/intl';
import { isTenantRoleLevel } from 'utils/utils';
import { DEBOUNCE_TIME } from 'utils/constants';
import ListTable from "./ListTable";
import SearchForm from "./SearchForm";
/**
 * 消息模板列表组件
 * @extends {Component} - React.Component
 * @reactProps {Object} [location={}] - 当前路由信息
 * @reactProps {Object} [match={}] - react-router match路由信息
 * @reactProps {!Object} messageTemplate - 数据源
 * @reactProps {!boolean} loading - 数据加载是否完成
 * @reactProps {Object} form - 表单对象
 * @reactProps {Function} [dispatch= e => e] - redux dispatch方法
 * @return React.element
 */

var List = (_dec = connect(function (_ref) {
  var messageTemplate = _ref.messageTemplate,
      loading = _ref.loading;
  return {
    messageTemplate: messageTemplate,
    loading: loading.effects['messageTemplate/fetchTemplate'],
    tenantRoleLevel: isTenantRoleLevel()
  };
}), _dec2 = formatterCollections({
  code: ['cmsg.messageTemplate', 'entity.tenant', 'entity.lang', 'cmsg.common']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Debounce(DEBOUNCE_TIME), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(List, _Component);

  var _super = _createSuper(List);

  function List(props) {
    var _this;

    _classCallCheck(this, List);

    _this = _super.call(this, props);
    _this.filterForm = void 0;
    _this.state = {
      isCopy: false
    };
    return _this;
  }

  _createClass(List, [{
    key: "componentDidMount",
    value:
    /**
     * componentDidMount 生命周期函数
     * render()执行后获取页面数据
     */
    function componentDidMount() {
      var _this$props = this.props,
          _this$props$messageTe = _this$props.messageTemplate.pagination,
          pagination = _this$props$messageTe === void 0 ? {} : _this$props$messageTe,
          _this$props$location$ = _this$props.location.state;
      _this$props$location$ = _this$props$location$ === void 0 ? {} : _this$props$location$;
      var _back = _this$props$location$._back; // 校验是否从详情页返回

      var page = _isUndefined(_back) ? {} : pagination;
      this.handleSearch(page);
    }
  }, {
    key: "handleRef",
    value: function handleRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.filterForm = ref.props.form;
    }
    /**
     * 新增模板，跳转到明细页面
     */

  }, {
    key: "handleAddTemplate",
    value: function handleAddTemplate() {
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cmsg/message-template/create"
      }));
    }
  }, {
    key: "handleSearchFormSearch",
    value: function handleSearchFormSearch(form) {
      this.handleSearch({}, form.getFieldsValue());
    }
    /**
     * 页面查询
     * @param {object} fields - 查询参数
     * @param {object} query - 查询参数
     */

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var dispatch = this.props.dispatch;
      var filterFormValues = this.filterForm ? this.filterForm.getFieldsValue() : {};
      dispatch({
        type: 'messageTemplate/fetchTemplate',
        payload: _objectSpread(_objectSpread({
          page: _isEmpty(fields) ? {} : fields
        }, filterFormValues), query)
      });
    }
    /**
     * 复制
     */

  }, {
    key: "handleCopy",
    value: function handleCopy() {
      var _this2 = this;

      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      this.setState({
        isCopy: true
      }, function () {
        var isCopy = _this2.state.isCopy;

        var payload = _objectSpread(_objectSpread({}, record), {}, {
          isCopy: isCopy
        });

        dispatch(routerRedux.push({
          pathname: "/cmsg/message-template/create",
          payload: payload
        }));
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete() {
      var _this3 = this;

      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageTemplate/deleteItem',
        payload: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this3.handleSearch();
        }
      });
    }
  }, {
    key: "handleUpdate",
    value: function handleUpdate() {
      var _this4 = this;

      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      this.setState({
        isCopy: false
      }, function () {
        var isCopy = _this4.state.isCopy;

        var payload = _objectSpread(_objectSpread({}, record), {}, {
          isCopy: isCopy
        });

        dispatch(routerRedux.push({
          pathname: "/cmsg/message-template/detail/".concat(record.templateId),
          payload: payload
        }));
      });
    }
    /**
     * render
     * @returns React.element
     */

  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          loading = _this$props2.loading,
          _this$props2$messageT = _this$props2.messageTemplate,
          _this$props2$messageT2 = _this$props2$messageT.list,
          list = _this$props2$messageT2 === void 0 ? [] : _this$props2$messageT2,
          _this$props2$messageT3 = _this$props2$messageT.pagination,
          pagination = _this$props2$messageT3 === void 0 ? {} : _this$props2$messageT3,
          _this$props2$messageT4 = _this$props2$messageT.language,
          language = _this$props2$messageT4 === void 0 ? [] : _this$props2$messageT4,
          tenantRoleLevel = _this$props2.tenantRoleLevel,
          path = _this$props2.match.path;
      var filterProps = {
        tenantRoleLevel: tenantRoleLevel,
        language: language,
        onSearch: this.handleSearchFormSearch,
        onRef: this.handleRef
      };
      var listProps = {
        tenantRoleLevel: tenantRoleLevel,
        pagination: pagination,
        language: language,
        loading: loading,
        path: path,
        dataSource: list,
        onChange: this.handleSearch,
        // onEditOrCopy: this.handleEditOrCopyContent,
        onCopy: this.handleCopy,
        onUpdate: this.handleUpdate,
        onDelete: this.handleDelete
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.messageTemplate.view.message.title.list').d('消息模板')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.create"),
          type: 'button',
          meaning: '消息模板-新建'
        }],
        icon: "plus",
        type: "primary",
        onClick: this.handleAddTemplate
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(SearchForm, filterProps)), /*#__PURE__*/React.createElement(ListTable, listProps)));
    }
  }]);

  return List;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "handleRef", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRef"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddTemplate", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddTemplate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearchFormSearch", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchFormSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCopy", [_dec7, _dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCopy"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleUpdate", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleUpdate"), _class2.prototype)), _class2)) || _class) || _class);
export { List as default };